package com.cb.spzx.manager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cb.spzx.manager.service.SysUserService;
import com.cb.spzx.model.dto.h5.UserResetPwdDto;
import com.cb.spzx.model.dto.system.AssginRoleDto;
import com.cb.spzx.model.dto.system.SysUserDto;
import com.cb.spzx.model.entity.system.SysUser;
import com.cb.spzx.model.vo.common.Result;
import com.cb.spzx.model.vo.common.ResultCodeEnum;
import com.cb.spzx.utils.AuthContextUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理")
@RestController
@RequestMapping(value = "/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "查询用户列表")
    @GetMapping(value = "/findByPage/{pageNum}/{pageSize}")
    public Result findByPage(SysUserDto sysUserDto ,
                             @PathVariable(value = "pageNum") Integer pageNum ,
                             @PathVariable(value = "pageSize") Integer pageSize){
        System.out.println("sysUserDto = " + sysUserDto + ", pageNum = " + pageNum + ", pageSize = " + pageSize);
        Page<SysUser> sysUserPage = new Page<>(pageNum, pageSize);
        sysUserService.findByPage(sysUserDto,sysUserPage);
        return Result.build(sysUserPage, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "添加用户")
    @PostMapping(value = "/saveSysUser")
    public Result saveSysUser(@RequestBody SysUser sysUser){
        int result=sysUserService.saveSysUser(sysUser);
        if(result>0){
            return Result.build(null,ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @Operation(summary = "修改用户")
    @PutMapping(value = "/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser){
        int result=sysUserService.updateSysUser(sysUser);
        if(result>0){
            return Result.build(null,ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @Operation(summary = "用户信息个人信息")
    @PutMapping(value = "/updateInfo")
    public Result updateInfo(@RequestBody SysUser sysUser,@RequestHeader(name = "token") String token){
        sysUserService.updateInfo(sysUser,token);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "修改个人密码")
    @PutMapping("/resetPwd")
    public Result resetPwd(@RequestBody UserResetPwdDto userResetPwdDto){
        sysUserService.resetPwd(userResetPwdDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping(value = "/deleteById/{userId}")
    public Result deleteById(@PathVariable(value = "userId") Long userId){
        int result=sysUserService.deleteById(userId);
        if(result>0){
            return Result.build(null,ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @Operation(summary = "为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleDto assginRoleDto){
        sysUserService.doAssign(assginRoleDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
